//
//  MKScrollLB.m
//  ManKeCartoon
//
//  Created by dllo on 15/10/21.
//  Copyright © 2015年 ManKe. All rights reserved.
//

#import "MKScrollLB.h"
#import "MKBaseHeader.h"
@implementation MKScrollLB
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self createScrollView];
        
    }
    return self;
}

#pragma mark - 创建录播图 并且从网络获取数据
-(void)createScrollView
{
    
    // afn获取数据
   [AFNETWORK getDataWithNoCacheString:@"http://mobilev3.ac.qq.com/Home/homePageDetailV2/timestamp/0/user_local_version/5.0.7/user_channel/2011/" block:^(id block) {
       

    

        
        [self LBView];
        
        _imageArray = [[NSMutableArray alloc] init];
        
        
        for (NSDictionary *dic in [[[block objectForKey:@"data"]objectForKey:@"banner"] objectForKey:@"data"]) {
            [_imageArray addObject:[dic objectForKey:@"banner_url"]];
        }
        
        
        
        // 滚动范围
        [_LBScrollView setContentSize:CGSizeMake((_imageArray.count + 2) * self.frame.size.width, 0)];
        
        for (int i = 0; i < _imageArray.count ; i++) {
            _LBimageView = [[UIImageView alloc] init];
            [_LBimageView sd_setImageWithURL:[NSURL URLWithString:_imageArray[i]]];
            [_LBimageView setFrame:CGRectMake((i + 1) * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
            [_LBScrollView addSubview:_LBimageView];
            
            
            
#pragma mark 做个假的滚动条
            _Line = [[UIView alloc] initWithFrame:CGRectMake(0, _LBimageView.frame.size.height - 2, self.frame.size.width / _imageArray.count * (i + 1), 2)];
            [_Line setBackgroundColor:[UIColor colorWithRed:0.92 green:0.4 blue:0.18 alpha:1]];
            
            [_LBimageView addSubview:_Line];
            
            
            
        }
        
        // 第一个 和最后一个  用于循环播出
        
        self.im1 = [[UIImageView alloc] init];
        [_im1 sd_setImageWithURL:[NSURL URLWithString:[_imageArray lastObject]]];
        [_im1 setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        
        [_LBScrollView addSubview:_im1];
        
        
        self.im2 = [[UIImageView alloc] init];
        [_im2 sd_setImageWithURL:[NSURL URLWithString:[_imageArray firstObject]]];
        [_im2 setFrame:CGRectMake(self.frame.size.width * (_imageArray.count + 1), 0, self.frame.size.width, self.frame.size.height)];
        
        [_LBScrollView addSubview:_im2];
        
        
        [_LBScrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];
        
        
        
        
        
        
        
    }];
    
    // 设置定时器 自动滚动
    [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(Timer) userInfo:nil repeats:YES];
    
    
    
    
}



//创建scrollView
-(void)LBView
{
    _LBScrollView = [[UIScrollView alloc] init];
    
    _LBScrollView.delegate = self;
    [_LBScrollView setPagingEnabled:YES];
    [_LBScrollView setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    _LBScrollView.bounces = NO;
    _LBScrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_LBScrollView];
    
    
    
}

#pragma mark - 自动滚动方法实现
-(void)Timer
{
    [_LBScrollView setContentOffset:CGPointMake(_LBScrollView.contentOffset.x + _LBScrollView.frame.size.width, 0) animated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if (scrollView.contentOffset.x == 0) {
        [scrollView setContentOffset:CGPointMake(_LBScrollView.frame.size.width * _imageArray.count, 0) animated:NO];
    }
    if (scrollView.contentOffset.x == _LBScrollView.frame.size.width * (_imageArray.count + 1)) {
        [scrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
    }
    
}



@end
